package com.joyxy.easy.gui;

import cn.sanenen.dm.base.DmApiHelper;
import com.joyxy.easy.entity.WindowContext;
import com.joyxy.easy.manger.WindowManager;
import com.joyxy.easy.monitor.AutomationRunner;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class GameAssistantUI extends JFrame {
    private final MainTabbedPane mainTabbedPane = new MainTabbedPane();
    private final Map<Long, JComponent> windowPanels = new HashMap<>();
    private JComboBox<WindowInfo> windowComboBox;

    public GameAssistantUI() {
        setTitle("快乐蜘蛛侠");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        initControlPanel();
        add(mainTabbedPane, BorderLayout.CENTER);

        setVisible(true);
    }

    private void initControlPanel() {
        JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        controlPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 刷新窗口按钮
        JButton refreshBtn = new JButton("刷新窗口");
        refreshBtn.addActionListener(e -> refreshWindowList());
        controlPanel.add(refreshBtn);

        // 窗口选择下拉框
        windowComboBox = new JComboBox<>();
        windowComboBox.setPreferredSize(new Dimension(200, 30));
        controlPanel.add(new JLabel("选择窗口:"));
        controlPanel.add(windowComboBox);

        // 添加窗口按钮
        JButton addBtn = new JButton("添加窗口");
        addBtn.addActionListener(e -> addSelectedWindow());
        controlPanel.add(addBtn);

        JButton deleteBtn = new JButton("删除窗口");
        deleteBtn.addActionListener(e -> deleteWindow());
        controlPanel.add(deleteBtn);


        add(controlPanel, BorderLayout.NORTH);


    }


    private void refreshWindowList() {
        windowComboBox.removeAllItems();
        String hwnds = DmApiHelper.getInstance().EnumWindow(0, "JoyXY", "", 15);
        for (String hwnd : hwnds.split(",")) {
            if (!hwnd.isEmpty()) {
                String title = DmApiHelper.getInstance().GetWindowTitle(Long.parseLong(hwnd));
                String processPath = DmApiHelper.getInstance().GetWindowProcessPath(Long.parseLong(hwnd));
                if (processPath.contains("client")) {
                    windowComboBox.addItem(new WindowInfo(Long.parseLong(hwnd), title));
                }
            }
        }
    }

    private void deleteWindow() {
        WindowInfo selected = (WindowInfo) windowComboBox.getSelectedItem();
        if (selected != null && windowPanels.containsKey(selected.hwnd)) {
            int i = mainTabbedPane.indexOfTab(selected.title);
            if (i >= 0) {
                long hwnd = selected.hwnd;
                WindowManager.unbindWindow(hwnd);
            }
            mainTabbedPane.remove(i);
            windowPanels.remove(selected.hwnd);
        }
    }

    private void addSelectedWindow() {
        WindowInfo selected = (WindowInfo) windowComboBox.getSelectedItem();
        if (selected != null && !windowPanels.containsKey(selected.hwnd)) {
            // 创建标签页内容
            JTabbedPane windowTab = new JTabbedPane();
            windowTab.addTab("状态监控", new StatusPanel(selected.hwnd));
            windowTab.addTab("基础设置", new BasicSettingPanel(selected.hwnd));

            // 添加到主界面
            mainTabbedPane.addTab(selected.hwnd, selected.title, windowTab);
            windowPanels.put(selected.hwnd, mainTabbedPane);
        }
    }

    private static class WindowInfo {
        final long hwnd;
        final String title;

        public WindowInfo(long hwnd, String title) {
            this.hwnd = hwnd;
            this.title = title;
        }

        @Override
        public String toString() {
            return title + " (HWND: " + hwnd + ")";
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new GameAssistantUI());
    }
}